package com.xzy.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager")DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();
//        投票，申请成为出题人随便访问，同意成为出题人必须拥有setTopic权限，出题必须拥有topic权限
        filterMap.put("/user/question","perms[user:topic]");
        filterMap.put("/user/AgreeAuthor/*","perms[user:agreeAuthor]");
        filterMap.put("/user/addTopic/*","perms[user:author]");
        filterMap.put("/user/sendEmail/*","perms[user:agreeAuthor]");
//        没有权限的会进入登录页面
        shiroFilterFactoryBean.setLoginUrl("/");
        shiroFilterFactoryBean.setUnauthorizedUrl("/noAuth");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(@Qualifier("voteUserRealm") VoteUserRealm voteUserRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(voteUserRealm);
        return securityManager;
    }
    @Bean(name = "voteUserRealm")
    public VoteUserRealm  getVoteUserRealm(){
        return new VoteUserRealm();
    }
}
